package com.wms.service.System;

import com.wms.common.PageInfo;
import com.wms.common.Utils;
import com.wms.dao.MenuMapper;
import com.wms.dao.SystemMapper;
import com.wms.dao.TableHeadMapper;
import com.wms.domain.Menu;
import com.wms.domain.TableHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SystemImpl implements Systeminface {
    private static final Logger log = LoggerFactory.getLogger(Systeminface.class);
    @Autowired
    private MenuMapper mapper;
    @Autowired
    private TableHeadMapper tableHeadMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Transactional
    @Override
    public Object addMenu(Menu menu) {
        log.info(menu.toString());
        menu.setCreate_date(Utils.getDate());
//        int value;
//        Response response = new Response();
        if(menu.getId() != null){
             mapper.update(menu);
        }else{
             mapper.addMenu(menu);
        }
//        response.setCode(value);
        return null;
    }

    @Override
    public Object getMenuPage(Menu menu) {
        log.info(menu.toString());
        PageInfo page = new PageInfo();
        page.setData(mapper.getMenuPage(menu));
        page.setCount(menu.getBaseCount());
        page.setSum(menu.getBaseSum());
        return page;
    }

    @Transactional
    @Override
    public Object addTable(List<TableHead> tableHead) {
        log.info(tableHead.toString());
//        if(tableHead.get(0).getId() == null){
//            for (TableHead head : tableHead) {
//                tableHeadMapper.addTable(head);
//            }
//        }else{
//            tableHeadMapper.delTable(tableHead.get(0).getTable_key());
//            for (TableHead head : tableHead) {
//                tableHeadMapper.addTable(head);
//            }
//        }
        if(tableHead.get(0).getId() != null){
            tableHeadMapper.delTable(tableHead.get(0).getTable_key());
        }
        for (TableHead th :tableHead){
            tableHeadMapper.addTable(th);
        }
        return null;
    }

    @Override
    public Object getTablePage(TableHead tableHead) {
        log.info(tableHead.toString());
        PageInfo page = new PageInfo();
        page.setData(tableHeadMapper.getTablePage(tableHead));
        page.setCount(tableHead.getBaseCount());
        page.setSum(tableHead.getBaseSum());
        return page;
    }

    @Override
    public Object getTableHead(String tableKey) {
        return tableHeadMapper.getTableHead(tableKey);
    }

    @Override
    public Object delMenu(Long id) {
        log.info(String.valueOf(id));
//        int value = mapper.delMenu(id);
//        Response response = new Response();
//        response.setCode(value);
        mapper.delMenu(id);
        return null;
    }


    @Override
    public Object getOptions() {
//        List<Map> list = systemMapper.getOptions();
        return systemMapper.getOptions();
    }
}
